import java.io.*;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

public class TfIdf {
    public static void main(String[] args) throws IOException {
        Caulate();
    }

    public static void Caulate() throws IOException {
        String src = "G:\\毕业论文\\数据集\\CountTaskVal.csv";
        BufferedReader bufferedReader = new BufferedReader(new FileReader(src));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("G:\\毕业论文\\数据集\\tfidf.csv"));
        //将数据读取出来存到二维数组矩阵中
        String line = null;
        String[][] taskVal = new String[4120][12];
        //定义一个指针，跟踪读到第几行了
        int index = 0;
        while ((line = bufferedReader.readLine()) != null) {
            String[] split = line.split(",");
            index++;
            //当浏览到第二行的时候，开始存储到数组中
            if (index > 1) {
                for (int i = 1; i < split.length; i++) {
                    //第一个数不存在数组中
                    taskVal[index - 2][i - 1] = split[i];
                }

            }
        }

        bufferedReader.close();
        //获取tf结果
        List<Integer> countList = countList(taskVal);
        String[][] tf = CalTf(taskVal, countList);
        //获取idf的结果
        List<String> idf = idf(taskVal);
        //计算tf-idf
        String[][] tfIdf = tfIdf(tf, idf);
        printArray(tfIdf);
//        for (String data:idf){
//            System.out.print(data+" ");
//        }
        //分别操作矩阵计算ti-idf
    }
    //统计每个人选择的任务数量
    public static List<Integer> countList(String[][] taskVal) {
        List<Integer> countList = new ArrayList<Integer>();
        int sum=0;
        for (int i = 0; i < taskVal.length; i++) {
            for (int j = 0; j < taskVal[i].length; j++) {
                if (taskVal[i][j] != null) {
                    sum+= Integer.parseInt(taskVal[i][j]);
                }
            }
            countList.add(sum);
            sum=0;
        }
        return countList;
    }
    //打印数组
    public static void printArray(String[][] taskVal) {
        for (String[] contents : taskVal) {
            for (String data : contents) {
                if (data != null)
                    System.out.print(data + " ");
            }
            System.out.println();
        }
    }

    //计算tf
    public static String[][] CalTf(String[][] taskVal,List<Integer> countList) {
        String[][] tf=new String[4120][12];
        for (int i = 0; i < taskVal.length; i++) {
            for (int j = 0; j < taskVal[i].length; j++) {
                if (taskVal[i][j] != null) {
                    int i1 = Integer.parseInt(taskVal[i][j]);
                    double i2 = i1;
                    DecimalFormat decimalFormat=new DecimalFormat("0.000");
                    String result = decimalFormat.format(i2 / countList.get(i));
                    //double m = i2 / taskVal.length;
                    tf[i][j] = result;
                }

            }
        }
        return tf;
        //printArray(taskVal);
    }
    //计算idf
    public static List<String> idf(String[][] taskVal){
        List<String> idf=new ArrayList<String>();
        int count=0;
        //控制列数
        for (int i = 0; i < taskVal[0].length; i++) {
            //控制行数
            for (int j = 0; j < taskVal.length; j++) {
                if (!taskVal[j][i].equals("0")) {
                   count++;
                }
            }
            DecimalFormat decimalFormat=new DecimalFormat("0.000");
            double tempTaskVal=taskVal.length;
            double temp=tempTaskVal/count;
            double v = Math.log10(temp);
            String format = decimalFormat.format(v);
            idf.add(format);
            count=0;

        }
        return idf;
    }
    //计算tf-idf的值
    public static String[][] tfIdf(String[][] tf,List<String> idf){
        for (int j=0;j<tf[0].length;j++){
            double tempIdf = Double.parseDouble(idf.get(j));
            for (int i=0;i<tf.length;i++){
                double tempTf = Double.parseDouble(tf[i][j]);
                double tempTfIdf=tempTf*tempIdf;
                DecimalFormat decimalFormat=new DecimalFormat("0.00000");
                tf[i][j]=decimalFormat.format(tempTfIdf);
            }
        }
    return tf;
    }
}

